#include "gameroom.h"
#include "GameHall.h"
#include "systemmsg.h"

IGameRoom::IGameRoom(IGameHall *hall,CGameInfo *info)
{
    _DEBUG_CHECK(hall);
    _DEBUG_CHECK(info);
    m_pHall = hall;
    m_pGameStation = NULL;
    m_hGameStationDll = NULL;

    m_GameInfo = *info;

    m_arrGameTable.resize( m_GameInfo.getTableCount() );
    for (int i = 0;i < m_arrGameTable.size();i++)
    {
        m_arrGameTable[i] = new IGameTable(this);
        m_arrGameTable[i]->setIndex(i);
    }
}

IGameRoom::~IGameRoom(void)
{
    int i = 0;
    for (PLAYERMAP::iterator it = m_mapPlayers.begin();it != m_mapPlayers.end();it++)
    {
        delete it->second;
    }
    m_mapPlayers.clear();

    for (i = 0;i < m_arrGameTable.size();i++)
    {
        delete m_arrGameTable[i];
    }
    m_arrGameTable.clear();

    if(m_pGameStation)
    {
        m_pGameStation->Release();
        m_pGameStation = NULL;
    }

    if(m_hGameStationDll)
    {
        FreeLibrary(m_hGameStationDll);
        m_hGameStationDll = NULL;
    }
}

bool IGameRoom::Initialize(LPCTSTR stationfile,const char*constr,HWND hReciver,DWORD nRecv,DWORD nClose)
{
    if(m_pGameStation != NULL)
    {
        if( m_pGameStation->Initialize(constr) )
        {
            m_pGameStation->SetReceiver(hReciver,nRecv,nClose);
            return true;
        }
        return false;
    }

    IeoString file = IeoFile::GetFullFileName( stationfile );
    m_hGameStationDll = LoadLibrary(file);//LoadLibraryEx(file,NULL,LOAD_WITH_ALTERED_SEARCH_PATH);
    if( m_hGameStationDll == NULL )
        return false;

    CREATESTATION create = (CREATESTATION)GetProcAddress(m_hGameStationDll,"Create");
    if(create == NULL)
    {
        FreeLibrary(m_hGameStationDll);
        m_hGameStationDll = NULL;
        return false;
    }

    try
    {
        m_pGameStation = create();
        if(NULL == m_pGameStation)
        {
            FreeLibrary(m_hGameStationDll);
            m_hGameStationDll = NULL;
            return false;
        }

        if( m_pGameStation->Initialize(constr) )
        {
            m_pGameStation->SetReceiver(hReciver,nRecv,nClose);
            return true;
        }

        return false;

    }
    catch(...)
    {
        FreeLibrary(m_hGameStationDll);
        m_hGameStationDll = NULL;
        return false;
    }
}

void IGameRoom::Release()
{
    delete this;
}

IPlayer * IGameRoom::getSelf()
{
    return m_pHall->getSelf();
}

void IGameRoom::QuestEnterTable(int table,int seat)
{
    IPlayer *self = getSelf();
    NetPacket<sPlayerEnterTable> enter(ePF_SYSTEM,eMSID_USERENTER_TABLE);
    enter.data.seat = seat;
    enter.data.table = table;
    enter.data.uid = self->getUID();
    m_pGameStation->SendPacket(&enter,sizeof(enter));
}

void IGameRoom::QuestLeaveTable()
{
    IPlayer *self = getSelf();
    NetPacket<sPlayerLeaveTable> leave(ePF_SYSTEM,eMSID_USERLEAVE_TABLE);
    leave.data.seat = self->getSeat();
    leave.data.table = self->getTable()->getIndex();
    leave.data.uid = self->getUID();
    m_pGameStation->SendPacket(&leave,sizeof(leave));
}

IPlayer* IGameRoom::AddPlayer(USER_ID uid)
{
    IPlayer *now = GetPlayer(uid);
    if(now) return now;

    IPlayer *player = new IPlayer;
    player->setUID(uid);
    m_mapPlayers[uid] = player;
    return player;
}

IPlayer*IGameRoom::GetPlayer(USER_ID uid)
{
    if(uid == getSelf()->getUID()) return getSelf();

    PLAYERMAP::iterator it = m_mapPlayers.find(uid);
    if(it == m_mapPlayers.end())
        return NULL;
    return it->second;
}

void IGameRoom::SubPlayer(USER_ID uid)
{
    PLAYERMAP::iterator it = m_mapPlayers.find(uid);
    if(it == m_mapPlayers.end())
        return;
    delete it->second;
    m_mapPlayers.erase(it);
}